﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Aletfa.LightManager.Editor;

namespace Aletfa.LightManager
{
    /// <summary>
    /// Interaction logic for EditorControl.xaml
    /// </summary>
    public partial class EditorControl : UserControl
    {
        private Dictionary<DamageSources, int> Armors { get; set; }
        private Dictionary<DamageSources, int> Shields { get; set; }
        private Dictionary<DamageSources, int> Resistances { get; set; }

        public event GenericEventHandler<Unit> UnitSaved;
        public event GenericEventHandler<ValidationArgs> NameCheckRequested;
        public event GenericEventHandler<bool> AllowSaveChanged;

        public EditorControl()
        {
            InitializeComponent();
        }

        public Unit DataSource
        {
            get { return this.Tag as Unit; }
            set
            {
                this.Tag = value;
                this.IsEnabled = (value != null);
                if (value == null) return;
                this.txtName.Text = value.Name;
                this.txtPlayerName.Text = value.PlayerName;
                this.txtLife.Text = value.LifeCount.ToString();
                this.chkIsEnemy.IsChecked = value.IsEnemy;

                this.cmbArmorTypes.Items.Clear();
                this.cmbShieldTypes.Items.Clear();
                this.cmbResistanceTypes.Items.Clear();
                this.Armors = new Dictionary<DamageSources, int>();
                this.Shields = new Dictionary<DamageSources, int>();
                this.Resistances = new Dictionary<DamageSources, int>();

                this.PopulateList(value.Armors, this.Armors, this.cmbArmorTypes);
                this.PopulateList(value.Shields, this.Shields, this.cmbShieldTypes);
                this.PopulateList(value.Resistances, this.Resistances, this.cmbResistanceTypes);

                this.cmbArmorTypes.SelectedIndex = 0;
                this.cmbShieldTypes.SelectedIndex = 0;
            }
        }

        private void PopulateList(Dictionary<DamageSources, int> source, Dictionary<DamageSources, int> current, ComboBox combo)
        {
            foreach (var item in source)
            {
                combo.Items.Add(new ComboBoxItem() { Content = item.Key.ToString(), Tag = item.Key, Foreground = (item.Value > 0) ? Brushes.Black : Brushes.LightGray });
                current.Add(item.Key, item.Value);
            }
        }

        private void cmbShieldTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
                if (this.Shields != null)
                    this.txtShield.Text = this.Shields[(DamageSources)((ComboBoxItem)e.AddedItems[0]).Tag].ToString();
        }

        private void cmbResistanceTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
                if (this.Resistances != null)
                    this.txtResistance.Text = this.Resistances[(DamageSources)((ComboBoxItem)e.AddedItems[0]).Tag].ToString();
        }

        private void cmbArmorTypes_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
                if (this.Armors != null)
                    this.txtArmor.Text = this.Armors[(DamageSources)((ComboBoxItem)e.AddedItems[0]).Tag].ToString();
        }

        private void ValidateText(TextBox textbox, Dictionary<DamageSources, int> list, ComboBox combobox)
        {
            int number = 0;
            bool success = Int32.TryParse(textbox.Text, out number);
            textbox.Background = (success) ? null : GraphicUtilities.BrushFromHtml("#FA5858");
            list[(DamageSources)((ComboBoxItem)combobox.SelectedValue).Tag] = (success) ? number : 0;
            ((ComboBoxItem)combobox.SelectedValue).Foreground = (number > 0) ? Brushes.Black : Brushes.LightGray;
        }

        private void txtShield_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.ValidateText(this.txtShield, this.Shields, this.cmbShieldTypes);
        }

        private void txtArmor_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.ValidateText(this.txtArmor, this.Armors, this.cmbArmorTypes);
        }

        private void txtResistance_TextChanged(object sender, TextChangedEventArgs e)
        {
            this.ValidateText(this.txtResistance, this.Resistances, this.cmbResistanceTypes);
        }

        public void Save()
        {
            this.DataSource.Name = this.txtName.Text;
            this.DataSource.PlayerName = this.txtPlayerName.Text;
            this.DataSource.IsEnemy = this.chkIsEnemy.IsChecked.Value;
            this.DataSource.Armors = new Dictionary<DamageSources, int>(this.Armors);
            this.DataSource.Resistances = new Dictionary<DamageSources, int>(this.Resistances);
            this.DataSource.Shields = new Dictionary<DamageSources, int>(this.Shields);
            this.DataSource.Initialize(Convert.ToInt32(this.txtLife.Text));

            if (this.UnitSaved != null)
                this.UnitSaved(this.DataSource);
        }

        private void txtLife_TextChanged(object sender, TextChangedEventArgs e)
        {
            int number = 0;
            bool success = Int32.TryParse(this.txtLife.Text, out number);
            this.txtLife.Background = (success) ? null : GraphicUtilities.BrushFromHtml("#FA5858");
            if (this.AllowSaveChanged != null)
                this.AllowSaveChanged(success);
        }

        private void txtName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.DataSource == null) return;
            if (this.NameCheckRequested == null) return;
            if (this.DataSource.Name.ToLower() == this.txtName.Text.ToLower()) return;
            ValidationArgs args = new ValidationArgs(this.txtName.Text);
            this.NameCheckRequested(args);
            this.txtName.Background = (args.IsValid) ? null : GraphicUtilities.BrushFromHtml("#FA5858");
            if (this.AllowSaveChanged != null)
                this.AllowSaveChanged(args.IsValid);
        }
    }
}
